#include "unp.h"
#include "user.h"

int register_user(struct user *u,int id,int socket)
{
	ssize_t n;
	char message[MAXLINE];
	char uname[10];
	char group_id[10];
	int gid;
	snprintf(message ,sizeof(message),"Please input your name :\n");
	Writen(socket,message,strlen(message));
	if((n=Readline(socket,uname,10))==0)
	{
		return 0;
	}
	else
	{
		uname[strcspn(uname,"\n")]='\0';
		
		u->user_id = id;
		strcpy(u->name,uname);
		u->socket = socket;
		
	}
	
	snprintf(message,sizeof(message),"please join a group or create a group\n");
	Writen(socket,message,strlen(message));
	if((n=Readline(socket,group_id,10))==0)
	{
		return 0;
	}
	else
	{
		if(sscanf(group_id,"%d",&gid)==1)
			u->group_id=gid;
		return 1;
	}
}
